package com.fengchao.api.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.web.context.request.async.DeferredResult;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.ApiSelectorBuilder;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.logging.Logger;

@Configuration
@EnableSwagger2
public class SpringfoxConfig {
    @Autowired
    private Environment env;

    @Bean
    public Docket demoApi() {
        ApiSelectorBuilder apiSelectorBuilder = new Docket(DocumentationType.SWAGGER_2)
                .groupName("开发API")
                .genericModelSubstitutes(DeferredResult.class)
                //  .genericModelSubstitutes(ResponseEntity.class)
                .useDefaultResponseMessages(false)
                .forCodeGeneration(false)
                .pathMapping("/")
                .select();
        if ("prod".equals(env.getProperty("spring.profiles.active"))) {
            apiSelectorBuilder.paths(PathSelectors.regex("/v0.*"));//拦截的接口
        } else {
            apiSelectorBuilder.paths(PathSelectors.regex("/v1.*"));//拦截的接口
        }
        return apiSelectorBuilder.build().apiInfo(demoApiInfo());
    }

    private ApiInfo demoApiInfo() {
        Contact contact = new Contact("王念", "", "");
        ApiInfo apiInfo = new ApiInfo("蜂超API接口",//大标题
                "REST风格API",//小标题
                "0.1",//版本
                "",
                contact,//作者
                "",//链接显示文字
                ""//网站链接
        );
        return apiInfo;
    }
}